//
//  PlatformCoinRechargeViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/9/9.
//

#import "PlatformCoinRechargeViewController.h"
#import "RechargeModel.h"

#define payTypeTag 100
#define amountTypeTag 1000

@interface PlatformCoinRechargeViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewTop;

@property (weak, nonatomic) IBOutlet UILabel *rechargeAccountLabel;
@property (weak, nonatomic) IBOutlet UILabel *myPlatformCoinLabel;

@property (weak, nonatomic) IBOutlet UIView *rechargeView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rechargeAmountViewHeight;
@property (weak, nonatomic) IBOutlet UILabel *rechargeAmountTitleLabel;

@property (weak, nonatomic) IBOutlet UITextField *amountTextField;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;

@property (nonatomic, assign) NSInteger currentPayType;//当前支付方式，默认0
@property (nonatomic, assign) NSInteger currentAmountType;//当前选择的充值金额，默认-1，即不选择
@property (nonatomic, strong) RechargeModel *rechargeModel;


@end

@implementation PlatformCoinRechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.centerTitle.text = @"平台币充值";
    self.rightBtn.frame = CGRectMake(SL_WINDOW_WIDTH - 60, UIDevice.vg_statusBarHeight, 60, UIDevice.vg_navigationBarHeight);
    [self.rightBtn setTitle:@"明细" forState:UIControlStateNormal];
    self.scrollViewTop.constant = UIDevice.vg_navigationFullHeight;
    
    for (NSInteger i = 0; i < 4; i++) {
        UIView *payView = [self.view viewWithTag:payTypeTag+i];
        payView.layer.cornerRadius = 10;
        payView.layer.masksToBounds = YES;
        payView.layer.borderWidth = 1;
        VHWeakSelf
        [payView addTapBlock:^(id  _Nonnull obj) {
            VHStrongSelf
            self.currentPayType = payView.tag-payTypeTag;
        }];
    }
    
    self.currentPayType = 0;
    [self refreshData];
}

- (void)setCurrentPayType:(NSInteger)currentPayType
{
    _currentPayType = currentPayType;
    
    [self setupPayUnselected];
    [self setupPaySelected:currentPayType];
}

- (void)setupPayUnselected
{
    for (NSInteger i = 0; i < 4; i++) {
        UIView *payView = [self.view viewWithTag:payTypeTag+i];
        payView.layer.borderColor = kColor888888.CGColor;
        UIButton *payBtn = (UIButton *)[self.view viewWithTag:payTypeTag+10+i];
        payBtn.selected = NO;
    }
}
- (void)setupPaySelected:(NSInteger)currentPayType
{
    UIView *payView = [self.view viewWithTag:payTypeTag+currentPayType];
    payView.layer.borderColor = kThemeTitleColor.CGColor;
    UIButton *payBtn = (UIButton *)[self.view viewWithTag:payTypeTag+10+currentPayType];
    payBtn.selected = YES;
}

- (void)rightBtnClick
{
    [[AppLogic sharedInstance] pushToWebViewVC:@"平台币明细" url:@"https://www.7pa.com/yhhnqply.html"];
}

- (void)refreshData
{
    //模拟请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        RechargeModel *model = [[RechargeModel alloc] init];
        model.beilvAmount = 10;
        NSMutableArray *tem_amountArray = [[NSMutableArray alloc] init];
        NSArray *amountArray = [[NSArray alloc] initWithObjects:@"10", @"30", @"50", @"100", @"500", @"1000", @"5000", @"10000", nil];
        for (NSString *amount in amountArray) {
            RechargeAmountModel *amountModel = [[RechargeAmountModel alloc] init];
            amountModel.recchargeAmount = amount;
            [tem_amountArray addObject:amountModel];
        }
        model.rechargeArray = tem_amountArray;
        self.rechargeModel = model;
        [self refreshUI];
    });
}


- (void) refreshUI
{
    self.rechargeAmountTitleLabel.text = [NSString stringWithFormat:@"充值数量(1元=%ld平台币)", self.rechargeModel.beilvAmount];
    
    CGFloat amountViewW = (SL_WINDOW_WIDTH - 20*4)/3;
    UIView *lastAmountView = nil;
    for (NSInteger i = 0; i < self.rechargeModel.rechargeArray.count; i++) {
        RechargeAmountModel *amountModel = self.rechargeModel.rechargeArray[i];
        UIView *amountView = [self.view viewWithTag:amountTypeTag+i];
        if (!amountView) {
            amountView = [[UIView alloc] init];
            amountView.layer.cornerRadius = 10;
            amountView.layer.masksToBounds = YES;
            amountView.layer.borderWidth = 1;
            amountView.tag = amountTypeTag+i;
            VHWeakSelf
            [amountView addTapBlock:^(id  _Nonnull obj) {
                VHStrongSelf
                [self.view endEditing:YES];
                self.currentAmountType = amountView.tag - amountTypeTag;
                [self selectAmount:amountModel.recchargeAmount];
            }];
            [self.rechargeView addSubview:amountView];
        }
        [amountView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(amountViewW);
            if (i % 3 == 0) {
                make.left.mas_equalTo(20);
            } else {
                make.left.equalTo(lastAmountView.mas_right).offset(20);
            }
            if (i / 3 == 0) {
                make.top.mas_equalTo(self.rechargeAmountTitleLabel.mas_bottom);
            } else {
                if (i % 3 == 0) {
                    make.top.equalTo(lastAmountView.mas_bottom).offset(20);
                } else {
                    make.top.equalTo(lastAmountView);
                }
            }
        }];
        UILabel *platformCionLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+20+i];
        if (!platformCionLabel) {
            platformCionLabel = [[UILabel alloc] init];
            platformCionLabel.tag = amountTypeTag+20+i;
            platformCionLabel.font = [UIFont boldSystemFontOfSize:18];
            [amountView addSubview:platformCionLabel];
        }
        platformCionLabel.text = [NSString stringWithFormat:@"%.0f", amountModel.recchargeAmount.doubleValue*self.rechargeModel.beilvAmount];
        [platformCionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(amountView);
            make.top.equalTo(amountView).offset(10);
        }];
        UILabel *wenbenLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+40+i];
        if (!wenbenLabel) {
            wenbenLabel = [[UILabel alloc] init];
            wenbenLabel.tag = amountTypeTag+40+i;
            wenbenLabel.text = @"平台币";
            wenbenLabel.font = [UIFont systemFontOfSize:12];
            [amountView addSubview:wenbenLabel];
        }
        [wenbenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(amountView);
            make.top.equalTo(platformCionLabel.mas_bottom).offset(5);
        }];
        UILabel *amountLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+60+i];
        if (!amountLabel) {
            amountLabel = [[UILabel alloc] init];
            amountLabel.tag = amountTypeTag+60+i;
            amountLabel.font = [UIFont systemFontOfSize:14];
            [amountView addSubview:amountLabel];
        }
        amountLabel.text = [NSString stringWithFormat:@"%@元", amountModel.recchargeAmount];
        [amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(amountView);
            make.top.equalTo(wenbenLabel.mas_bottom).offset(5);
        }];
        
        [amountView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(amountLabel.mas_bottom).offset(10);
        }];
        
        lastAmountView = amountView;

    }
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    self.rechargeAmountViewHeight.constant = CGRectGetMaxY(lastAmountView.frame) + 10;
    
    self.currentAmountType = -1;
}

- (void)setCurrentAmountType:(NSInteger)currentAmountType
{
    _currentAmountType = currentAmountType;
    
    [self setupAmountUnselected];
    if (currentAmountType != -1) {
        [self setupAmountSelected:currentAmountType];
    }
    
}
- (void)setupAmountUnselected
{
    for (NSInteger i = 0; i < self.rechargeModel.rechargeArray.count; i++) {
        UIView *amountView = [self.view viewWithTag:amountTypeTag+i];
        amountView.backgroundColor = [UIColor whiteColor];
        amountView.layer.borderColor = kColor888888.CGColor;
        UILabel *platformCionLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+20+i];
        platformCionLabel.textColor = [UIColor blackColor];
        UILabel *wenbenLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+40+i];
        wenbenLabel.textColor = [UIColor blackColor];
        UILabel *amountLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+60+i];
        amountLabel.textColor = kColor888888;
    }
}
- (void)setupAmountSelected:(NSInteger)currentAmountType
{
    UIView *amountView = [self.view viewWithTag:amountTypeTag+currentAmountType];
    amountView.backgroundColor = kRGBColor(255, 247, 242);
    amountView.layer.borderColor = kThemeTitleColor.CGColor;
    UILabel *platformCionLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+20+currentAmountType];
    platformCionLabel.textColor = kThemeTitleColor;
    UILabel *wenbenLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+40+currentAmountType];
    wenbenLabel.textColor = kThemeTitleColor;
    UILabel *amountLabel = (UILabel *)[self.view viewWithTag:amountTypeTag+60+currentAmountType];
    amountLabel.textColor = kThemeTitleColor;
}

- (void)selectAmount:(NSString *)amount
{
    self.amountTextField.text = amount;
    [self.payBtn setTitle:[NSString stringWithFormat:@"支付%@元", amount] forState:UIControlStateNormal];
}

- (void)textFieldDidChangeSelection:(UITextField *)textField
{
    [self selectAmount:textField.text];
    for (NSInteger i = 0; i < self.rechargeModel.rechargeArray.count; i++) {
        RechargeAmountModel *amountModel = self.rechargeModel.rechargeArray[i];
        if (amountModel.recchargeAmount.integerValue == textField.text.integerValue) {
            self.currentAmountType = i;
            return;
        }
    }
    self.currentAmountType = -1;
}

- (IBAction)payBtnClick:(id)sender {
    NSLog(@"%@", self.payBtn.titleLabel.text);
}


@end
